
package uk.addie.mp3fixer.visitors;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.util.logging.Level;

import uk.addie.mp3fixer.MP3Fixer;
import uk.addie.mp3fixer.MP3Handle;
import uk.addie.mp3fixer.Util;

public class MoveAllToId3Folder extends AbstractVisitor {
    @Override protected String buttonName() {
        return "Refolder all files from ID3";
    }

    @Override protected void modifyMp3(final MP3Handle mp3, final Path file) {
        String artistName = mp3.getArtist();
        if (mp3.isCompilation()) {
            artistName = "Compilation";
        }
        final Path targetDir = MP3Fixer.getRoot().resolve(Util.clean(artistName)).resolve(Util.clean(mp3.getAlbum()));
        targetDir.toFile().mkdirs();
        Util.moveFile(file, targetDir.resolve(file.getFileName()));
    }

    @Override public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
        final File directory = dir.toFile();
        if (directory.isDirectory()) {
            directory.delete();
        } else {
            MP3Fixer.LOG.log(Level.SEVERE, "!!! not a directory:" + dir.toString());
        }
        return FileVisitResult.CONTINUE;
    }
}